package P2PChat;
/**
 * 主登录界面
 * */
import com.sun.java.accessibility.util.AWTEventMonitor;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;



public class MainFrame extends Frame {
    TextArea sentBox = new TextArea(3, 10);

    TextArea reciveBox = new TextArea(10, 10);
    JButton bSent = new JButton("发送");


    public MainFrame()  {
        //对两个按钮进行监听，点击就输出ok、cancel
        bSent.addActionListener(new MainFrameListener());

        //实现回车键发送（有多一个空行的bug）
        sentBox.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {
                if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)
                {
                    // 模拟按键发送消息
                    bSent.doClick();
                    //机器人类实现光标的回到原处
                    Robot robot = null;
                    try {
                        robot = new Robot();
                    } catch (AWTException awtException) {
                        awtException.printStackTrace();
                    }
                    robot.keyPress(KeyEvent.VK_HOME);
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });

        this.setBounds(300, 200, 400, 300);
        reciveBox.setEditable(false);
        reciveBox.setBackground(Color.WHITE);
//        this.add(sentBox, BorderLayout.NORTH);
//        this.add(reciveBox, BorderLayout.CENTER);
        this.add(sentBox, BorderLayout.CENTER);
        this.add(reciveBox, BorderLayout.NORTH);
        this.add(bSent, BorderLayout.SOUTH);
        this.setTitle("聊天系统");
        //添加WindowEventListener(独立写的关闭)类
        this.addWindowListener(new WindowEventListener());
        this.setVisible(true);//这句话要记得注释

    }
    public void recive()throws Exception{
        ServerSocket serversocket=new ServerSocket(10003);
        while (true){
            Socket socket=serversocket.accept();
            DataInputStream din=new DataInputStream(socket.getInputStream());//使用了数据流接收数据
            String content=din.readUTF();
            reciveBox.append("Other:\n"+content+"\n\n");
            din.close();
            socket.close();
        }
    }
        class MainFrameListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
//                    System.out.println("发送按钮");
                try {
                    String sentString =sentBox.getText();
                    reciveBox.append("Me:\n"+sentString+"\n\n");
                    Socket socket=new Socket("127.0.0.1",10003);
                    DataOutputStream dos=new DataOutputStream(socket.getOutputStream());//使用数据流
                    dos.writeUTF(sentString);
                    dos.flush();
//                System.out.println("将发送内容发送给对方");
                    sentBox.setText("");

//                    sentBox.setCaretPosition();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        }


    }



